import { memo, useEffect } from 'react';
import type { FC, PropsWithChildren } from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { message } from 'antd';
import { isLoginState } from '@/store/authStore';
import { useRecoilValue } from 'recoil';

const AuthWrapper: FC<PropsWithChildren<{}>> = ({ children }) => {
  const isLogin = useRecoilValue(isLoginState);
  const location = useLocation();
  let redirect = `/login?redirect=${location.pathname}`;
  useEffect(() => {
    if (!isLogin) {
      message.warn('请先登录！');
    }
  }, []);
  return isLogin ? <>{children}</> : <Navigate to={redirect} replace />;
};

export default memo(AuthWrapper);
